package com.cmj.controller;


import com.cmj.Utils.SystemConverter;
import com.cmj.domain.ResponseResult;
import com.cmj.domain.dto.MenuDto;
import com.cmj.domain.entity.Menu;
import com.cmj.domain.vo.MenuTreeVo;
import com.cmj.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单权限表(Menu)表控制层
 *
 * @author makejava
 * @since 2022-09-04 09:27:20
 */
@RestController
@RequestMapping("/system/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @GetMapping("/treeselect")
    public ResponseResult<Menu> treeSelect(){
        ResponseResult menuList = menuService.getMenuList(new MenuDto());
        List<Menu> menus = (List<Menu>) menuList.getData();
        List<MenuTreeVo> menuTreeVos = SystemConverter.builderMenuTreeVo(menus);
        return ResponseResult.okResult(menuTreeVos);
    }

    @GetMapping("list")
    public ResponseResult getMenuList(MenuDto menuDto){
        return menuService.getMenuList(menuDto);
    }

    @PostMapping
    public ResponseResult<Menu> addMenu(@RequestBody Menu menu){
        return menuService.addMenu(menu);
    }

    @DeleteMapping("{ids}")
    public ResponseResult delMenu(@PathVariable("ids") Integer[] ids){
        return menuService.delMenu(ids);
    }

    @GetMapping("{id}")
    public ResponseResult getMenu(@PathVariable("id") Long id){
        return menuService.getMenu(id);
    }

    @PutMapping
    public ResponseResult updateMenu(@RequestBody Menu menu){
        return menuService.updateMenu(menu);
    }

    @GetMapping("/roleMenuTreeselect/{id}")
    public ResponseResult getUserMenuTree(@PathVariable("id") Long id){
        return menuService.getUserMenuTree(id);
    }
}

